﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillRepurchaseGoodsService
    {
        /// <summary>
        /// 查询采购退货单商品
        /// </summary>
        /// <param name="id">唯一ID</param>
        /// <returns></returns>
        public BillRepurchaseGoods GetBillRepurchaseGoods(long id)
        {
            BillRepurchaseGoods repurchaseGoods = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    repurchaseGoods = new BillRepurchaseGoods();
                    repurchaseGoods.Id = id;
                    repurchaseGoods = new BillRepurchaseGoodsDAO().GetBillRepurchaseGoods(conn, repurchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return repurchaseGoods;
        }

        /// <summary>
        /// 查询采购退货单商品列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面大小</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="repurchaseno">退货单号</param>
        /// <param name="purchaseno">采购单号</param>
        /// <param name="supplierName">供应商</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="ascfield">升序字段</param>
        /// <param name="descfield">降序字段</param>
        /// <returns></returns>
        public DataTable GetBillRepurchaseGoodsTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String repurchaseno, String purchaseno,
            String supplierName, String barcode, String adminUsername, String startDate, String endDate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillRepurchaseGoods repurchaseGoods = new BillRepurchaseGoods();
                    repurchaseGoods.Pageindex = pageindex;
                    repurchaseGoods.Pagesize = pagesize;
                    repurchaseGoods.EnterpriseNo = enterpriseNo;
                    repurchaseGoods.StoreNo = storeNo;
                    repurchaseGoods.RepurchaseNo = repurchaseno;
                    repurchaseGoods.PurchaseNo = purchaseno;
                    repurchaseGoods.SupplierName = supplierName;
                    repurchaseGoods.Barcode = barcode;
                    repurchaseGoods.AdminUsername = adminUsername;
                    repurchaseGoods.StartDate = startDate;
                    repurchaseGoods.EndDate = endDate;
                    repurchaseGoods.Ascfield = ascfield;
                    repurchaseGoods.Descfield = descfield;
                    dt = new BillRepurchaseGoodsDAO().GetBillRepurchaseGoodsTable(conn, repurchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询采购退货商品记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="repurchaseno">退货单号</param>
        /// <param name="purchaseno">采购单号</param>
        /// <param name="supplierName">供应商</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns></returns>
        public long GetBillRepurchaseGoodsRecord(String enterpriseNo, String storeNo, String repurchaseno, String purchaseno,
            String supplierName, String barcode, String adminUsername, String startDate, String endDate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillRepurchaseGoods repurchaseGoods = new BillRepurchaseGoods();
                    repurchaseGoods.EnterpriseNo = enterpriseNo;
                    repurchaseGoods.StoreNo = storeNo;
                    repurchaseGoods.RepurchaseNo = repurchaseno;
                    repurchaseGoods.PurchaseNo = purchaseno;
                    repurchaseGoods.SupplierName = supplierName;
                    repurchaseGoods.Barcode = barcode;
                    repurchaseGoods.AdminUsername = adminUsername;
                    repurchaseGoods.StartDate = startDate;
                    repurchaseGoods.EndDate = endDate;
                    record = new BillRepurchaseGoodsDAO().GetBillRepurchaseGoodsRecord(conn, repurchaseGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入采购退货商品
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="repurchaseGoods"></param>
        /// <returns></returns>
        public long InsertBillRepurchaseGoods(DbConnection conn, BillRepurchaseGoods repurchaseGoods)
        {
            long id = 0;
            try
            {
                id = new BillRepurchaseGoodsDAO().InsertBillRepurchaseGoods(conn, repurchaseGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 删除采购退货商品
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="repurchaseno"></param>
        /// <returns></returns>
        public int DeleteBillRepurchaseGoods(DbConnection conn, String repurchaseno)
        {
            int count = 0;
            try
            {
                BillRepurchaseGoods repurchaseGoods = new BillRepurchaseGoods();
                repurchaseGoods.RepurchaseNo = repurchaseno;
                count = new BillRepurchaseGoodsDAO().DeleteBillRepurchaseGoods(conn, repurchaseGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
